package com.bsoft.gol.hcb.service.feedback.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.docteam.DocTeamMemberEntity;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import hcn.feedback.DocTeamFeedbackType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:FangZhiyi
 * @Description
 * @Data Created in 2020/8/20 17:15
 * @Moddified By:
 */
public abstract class DocTeamFeedbackTypeDAO extends HibernateSupportDelegateDAO<DocTeamFeedbackType> {
    @DAOMethod(sql = "DELETE from DocTeamFeedbackType where medical_team_id =:teamId ")
    public abstract void deleteTeamFeedbackTypeByTeamId(@DAOParam("teamId") Long medicalTeamId);

    @DAOMethod(sql = " from  DocTeamFeedbackType where medical_team_id =:teamId ")
    public abstract List<DocTeamFeedbackType> queryDocTeamFeedbackTypeList(@DAOParam("teamId") Long medicalTeamId);

    public List<String> getClassifyListByDoctorId(String docId, String doctorId) {
        //docId和doctorId不能同时为空
        if (StringUtils.isEmpty(docId) && StringUtils.isEmpty(doctorId)) {
            return null;
        }
        HibernateStatelessResultAction<List<String>> action =
                new AbstractHibernateStatelessResultAction<List<String>>() {
                    @Override
                    public void execute(StatelessSession ss) {
                        StringBuffer stringBuffer = gettClassifyListStringBuffer(docId, doctorId);
                        Query sqlQuery = getQuery(ss, docId, doctorId, stringBuffer);
                        sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<Map> dbList = sqlQuery.list();
                        setResult(mapToList(dbList));
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    private StringBuffer gettClassifyListStringBuffer(String docId, String doctorId) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(
                "SELECT feedback_type from doc_team_feedback_type where medical_team_id IN (SELECT medical_team_id" +
                        " FROM doc_team_member ");
        if (StringUtils.isNotEmpty(docId)) {
            stringBuffer.append("WHERE doc_id=:docId");
        } else if (StringUtils.isNotEmpty(doctorId)) {
            stringBuffer.append("WHERE doctor_id=:doctorId");
        }
        stringBuffer.append(")");
        return stringBuffer;
    }

    private Query getQuery(StatelessSession ss, String docId, String doctorId, StringBuffer stringBuffer) {
        Query sqlQuery = ss.createSQLQuery(stringBuffer.toString());
        if (StringUtils.isNotEmpty(docId)) {
            sqlQuery.setParameter("docId", docId);
        }
        if (StringUtils.isNotEmpty(doctorId)) {
            sqlQuery.setParameter("doctorId", doctorId);
        }
        return sqlQuery;
    }

    private List<String> mapToList(List<Map> maps) {
        List<String> classifyList = new ArrayList<>();
        for (Map map : maps) {
            classifyList.add(map.get("feedback_type").toString());
        }
        return classifyList;
    }
//    @DAOMethod(sql = " FROM DocTeamMemberEntity WHERE medical_team_id IN (SELECT medical_team_id medicalTeamId FROM DocTeamFeedbackType WHERE feedback_type=:classify) ")
//    public abstract List<DocTeamMemberEntity> getDocCodeList(@DAOParam("classify") String classify);

    public List<DocTeamMemberEntity> getDoctorList(String classify) {
        HibernateStatelessResultAction<List<DocTeamMemberEntity>> action = new AbstractHibernateStatelessResultAction<List<DocTeamMemberEntity>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getDoctorListStringBuffer();
                Query sqlQuery = getDoctorListQuery(ss, classify, stringBuffer);
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> docList = sqlQuery.list();
                setResult(mapToDocList(docList));
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    private StringBuffer getDoctorListStringBuffer() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("SELECT doc_id doctorId, doctor_name doctorName,telephone telephone from doc_team_member where medical_team_id IN (SELECT medical_team_id FROM doc_team_feedback_type WHERE feedback_type=:classify)");
        return stringBuffer;
    }

    private Query getDoctorListQuery(StatelessSession ss, String classify, StringBuffer stringBuffer) {
        Query sqlQuery = ss.createSQLQuery(stringBuffer.toString());
        sqlQuery.setParameter("classify", classify);
        return sqlQuery;
    }

    private List<DocTeamMemberEntity> mapToDocList(List<Map> maps) {
        List<DocTeamMemberEntity> doctorList = new ArrayList<>();
        for (Map map : maps) {
            doctorList.add(ConversionUtils.convert(map, DocTeamMemberEntity.class));
        }
        return doctorList;
    }
}
